package com.maoye.ossp.sys.site.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.site.entity.Site;
import com.maoye.ossp.sys.site.entity.SiteNav;
import com.maoye.ossp.sys.site.entity.SiteNavMenu;
import com.maoye.ossp.sys.site.service.ISiteNavMenuService;
import com.maoye.ossp.sys.site.service.ISiteNavService;
import com.maoye.ossp.sys.site.service.ISiteService;
import com.maoye.ossp.sys.site.utils.BeanToMap;

/**
 * 站点导航Contorller
 * @author lijun
 *
 */
@Controller
@RequestMapping(value = "sys/sitenav/")
public class SiteNavContorller extends BaseController {
	
	
	/**
	 * 是否是下拉导航 (1:是,其它:不是)
	 */
	private static final int IS_DROPDOWN_YES = 1;
	
	/**
	 * 是否显示 (1:是,其它:不是)
	 */
	private static final int IS_SHOW_YES = 1;

	@Resource
	private ISiteService siteService;
	
	@Resource
	private ISiteNavService siteNavService;
	
	@Resource
	private ISiteNavMenuService siteNavMenuService;

	@RequestMapping(value = "list")
	public String list(SiteNav siteNav,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<SiteNav> list=siteNavService.getList(BeanToMap.beanToMap(siteNav));
		model.addAttribute("list", list);
		model.addAttribute("siteNav", siteNav);
		return "/sys/site/sitenav/list";
	}
	
	/**
	 * 获取树数据
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "getNode")
	public void getzNodes(HttpServletRequest request, HttpServletResponse response) throws IOException{
		long siteId = Long.parseLong(request.getParameter("siteId"));
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		SiteNav siteNav = new SiteNav();
		siteNav.setSiteId(siteId);
		String json = siteNavService.getzNodes(BeanToMap.beanToMap(siteNav));
		out.write(json);
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "view")
	public String view(HttpServletRequest request, Model model,SiteNavMenu siteNavMenu) {
		//获取当前导航信息
		long siteNavId = Long.parseLong(request.getParameter("siteNavId"));
		SiteNav siteNav = siteNavService.get(siteNavId);
		SiteNav SiteNavp = siteNavService.get(siteNav.getParentId());
		if(SiteNavp != null){
			siteNav.setParentName(SiteNavp.getName());
		}
		Site site = siteService.get(siteNav.getSiteId());
		if(site != null){
			siteNav.setSiteName(site.getName());
		}
		//获取导航下的菜单
		siteNavMenu.setSiteNavId(siteNavId);
		List<SiteNavMenu> list= siteNavMenuService.getList(BeanToMap.beanToMap(siteNavMenu));
		model.addAttribute("list", list);
		model.addAttribute("siteNavMenu", siteNavMenu);
		model.addAttribute("siteNav", siteNav);
		return "/sys/site/sitenav/view";
	}
	
	@RequestMapping(value = "edit")
	public String edit(HttpServletRequest request,Model model) {
		long siteNavId = Long.parseLong(request.getParameter("siteNavId"));
		SiteNav siteNav = siteNavService.get(siteNavId);
		SiteNav siteNavp = siteNavService.get(siteNav.getParentId());
		if(siteNavp != null){
			siteNav.setParentName(siteNavp.getName());
		}
		model.addAttribute("siteNav", siteNav);
		return "/sys/site/sitenav/add";
	}
	
	@RequestMapping(value = "add")
	public String add(SiteNav siteNav,Model model) {
		siteNav.setId(0L);
		siteNav.setIsDropdown(IS_DROPDOWN_YES);
		siteNav.setIsShow(IS_SHOW_YES);
		model.addAttribute("siteNav", siteNav);
		return "/sys/site/sitenav/add";
	}
	
	@RequestMapping(value = "save")
	public String save(SiteNav siteNav, Model model,
			RedirectAttributes redirectAttributes) {
		if (siteNav.getId() == 0) {
			siteNav.setId(idGenerator.getUId());
			siteNavService.create(siteNav);
		} else {
			siteNavService.update(siteNav);
		}
		addMessage(redirectAttributes, "保存数据成功");
		return "redirect:/sys/site/"+siteNav.getSiteId()+"/view";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Model model,HttpServletRequest request,RedirectAttributes redirectAttributes) throws IOException{
		long siteId = Long.parseLong(request.getParameter("siteId"));
		long siteNavId = Long.parseLong(request.getParameter("siteNavId"));
		siteNavService.remove(siteNavId);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/site/"+siteId+"/view";
	}
	
}
